//
//  WBTabBar.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/9.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit
import SnapKit

protocol  WBTabBarDelegate:NSObjectProtocol{
    
    func tabBarBtnClick()
    
}

class WBTabBar: UITabBar {

    weak var tabBarDelegate:WBTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    //设置界面
    func setupUI(){
        
        //懒加载按钮
        addSubview(tabBar)
        
        
    }

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //每个按钮的宽度
        let tabBarBtnW = self.frame.width * 0.2
        
        var index:CGFloat = 0
        
        for btn in self.subviews{
            
            if(btn.isKindOfClass(NSClassFromString("UITabBarButton")!)){
            
                btn.frame.origin.x = tabBarBtnW * index
                
                btn.frame.size.width = tabBarBtnW
                
                index++
                
                if index == 2{
                
                    index++
                
                
                }

            }
            
        }

        //设置frame
        tabBar.center = CGPointMake(self.frame.width * 0.5, self.frame.height * 0.5)
    }
    
    
    lazy var tabBar: UIButton = {
        let btn = UIButton()
        
        //添加按钮点击事件
        btn.addTarget(self, action: #selector(WBTabBar.btnClick), forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        //自动适应大小
        btn.sizeToFit()
        
        return btn
        
    }()
    
    
    
    func btnClick() {
        
        //代理调用方法
        tabBarDelegate?.tabBarBtnClick()
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
